package yz.dut.edu.graduate.service;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yz.dut.edu.graduate.bean.Result;
import yz.dut.edu.graduate.pojo.History;

import java.util.Date;
import java.util.Map;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class ComprehensiveService2 {

    @Autowired
    HistoryService historyService;

    @Autowired
    History_DishService history_dishService;

    @Autowired
    DishService dishService;

    public Result BuildHistory(Map<String,Integer> dishs,String position,String remark,float order_price){
        Date date=new Date();
        Result result;
        result=historyService.InsertHistory(position,date,remark,order_price);
        if(!result.isSuccess()){
            return result;
        }
        long h_id=(long)result.getDetails();
        for(Map.Entry<String,Integer> entry : dishs.entrySet()){
            int d_id=dishService.SearchByDishName(entry.getKey());
            if(d_id==-1){
                result.setMsg("失败原因：没找到菜");
                result.setSuccess(false);
                return result;
            }
            int row=history_dishService.InsertHistoryAndDish(h_id,d_id,entry.getValue());
            if(row==-1){
                result.setMsg("历史记录与菜品映射添加失败");
                result.setSuccess(false);
                return result;
            }
        }
        return result;
    }

    public Result AffirmCheck(long h_id,float real_price){
        return historyService.AffirmCheck(h_id,real_price);
    }


    public PageInfo<History> SelectPage(int pageNum, int pageSize){
        return historyService.SelectPage(pageNum,pageSize);
    }

    public Result SearchRange(String start, String end){
        return historyService.SearchRange(start,end);
    }

    public Result SelectHistoryDetailsByHid(long h_id){
        return historyService.SelectHistoryDetailsByHid(h_id);
    }
}
